
package com.linkage.educloud.ah.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.linkage.educloud.ah.data.CursorHelper;
import com.linkage.educloud.ah.data.Ws;
import com.linkage.educloud.ah.util.CharacterParser;
import com.yanzhenjie.nohttp.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class ClazzGroup implements Parcelable {
    /**
	 * 
	 */
    private static final long serialVersionUID = 447950432504783651L;

    /**
     * 联系人群组类型
     */
    public long group_id;

    public String group_name;
    
    public String school_name;

    public int group_members_count;
    public String desc;
    public int role;
    public int tab;
    public long classid;
    public String remoteId;
    public int type;
    public int is_shortdn;
    public int teacher_num;
    public int parent_num;
    public int studentnum;
    public int isopen_phone;

    public List<ClazzContact> group_members = new ArrayList<ClazzContact>();

    public static List<ClazzGroup> fromJsonObject(JSONObject json) throws JSONException
    {

        JSONArray grouplist = json.getJSONArray("list");
        int size = grouplist.length();

        List<ClazzGroup> mGroups = new ArrayList<ClazzGroup>(size);

        for (int i = 0; i < size; i++)
        {
            ClazzGroup groupItem = new ClazzGroup();
            JSONObject item = grouplist.getJSONObject(i);
            groupItem.group_name = item.getString("name");
            groupItem.school_name = item.getString("school_name");
            groupItem.group_members_count = item.getInt("count");
//            groupItem.desc = item.getString("desc");
            groupItem.role = item.getInt("role");
            groupItem.is_shortdn = item.getInt("is_shortdn");
            groupItem.remoteId = item.getString("newclassid");
            groupItem.studentnum = item.getInt("studentnum");
            if (item.getInt("type")==0) {
            	groupItem.classid = item.getInt("classid");
            	groupItem.group_id = item.getLong("classid");
			}else{
				groupItem.classid = item.getInt("id");
            	groupItem.group_id = item.getLong("id");
			}
//            groupItem.has_shortdn = item.getInt("has_shortdn");
            groupItem.type = item.getInt("type");
            
//            if (item.getInt("type")!=0) {//因为群组联系人和班级联系人重复，所以去掉群组联系人
//				continue;
//			}
            JSONArray memberlist = item.getJSONArray("memberlist");// 添加联系人
            int membersize = memberlist.length();
            int tNum = 0;
            int pNum = 0;
            for (int j = 0; j < membersize; j++) {

                JSONObject obj = memberlist.getJSONObject(j);
                ClazzContact memberItem = new ClazzContact();
                memberItem.id = obj.getLong("userid");
                memberItem.name = obj.getString("name");
                memberItem.type = obj.getInt("type");
                if (memberItem.type == 2) {
                    memberItem.firstName = ".";
                } else {
                    memberItem.firstName = CharacterParser.getInstance().getSelling(memberItem.name).substring(0, 1).toUpperCase();
                }
                memberItem.role = obj.getInt("role");
                memberItem.remoteId = obj.getString("remoteid");
                memberItem.invite = obj.getInt("login_type");
                memberItem.studentid = obj.getInt("studentid");
                memberItem.studentname = obj.getString("studentname");
                memberItem.student_relation = obj.getString("student_relation");
                memberItem.isopen_phone = obj.getInt("isopen_phone");
                if (memberItem.type == 1) {
                	pNum++;
				}else if(memberItem.type == 2){
					tNum++;
				}
                //这边服务端加了 dn 字段 暂时没有 手动捕捉 异常下
                try {
					if(!TextUtils.isEmpty(obj.getString("dn"))){
						memberItem.dn = obj.getString("dn");
					}else{
						memberItem.dn ="";
					}
					if(!TextUtils.isEmpty( obj.getString("short_dn"))&& (obj.getString("short_dn").length()>3) ){
						memberItem.short_dn = obj.getString("short_dn");
					}else{
						memberItem.short_dn ="";
					}
				} catch (Exception e) {
					memberItem.dn ="";
					memberItem.short_dn ="";
				}
                if (item.getInt("type")==0) {
                	memberItem.classid = item.getLong("classid");
				}else{
					memberItem.classid = item.getLong("id");
				}
                memberItem.newclassid = item.getLong("newclassid");
                //这边服务端加了 avatar_url 字段 暂时没有 手动捕捉 异常下
                try {
					if(null!= obj.getString("mid") ){
						memberItem.avatar_url = TextUtils.isEmpty(obj.getString("mid"))?"":obj.getString("mid");
					}else{
						memberItem.avatar_url ="";
					}
				} catch (Exception e) {
					memberItem.avatar_url ="";
				}
//                memberItem.avatar_url = obj.getString("avatar_url");
                groupItem.group_members.add(memberItem);
            }
            groupItem.teacher_num = tNum;
            groupItem.parent_num = pNum;
            mGroups.add(groupItem);
//            L.w("ClazzWorkContactGroup", " clazz id = "+groupItem.classid + " is_shortdn = "+groupItem.is_shortdn);
        }

        return mGroups;
    }

    private boolean isSelected;

    public boolean isSelected()
    {
        return isSelected;
    }

    public void setSelected(boolean isSelected)
    {
        this.isSelected = isSelected;
    }
    public static ClazzGroup fromCursor(CursorHelper helper){
        ClazzGroup group = new ClazzGroup();
        group.group_id = helper.getLong(Ws.GroupTable.ID);
        group.classid = helper.getLong(Ws.GroupTable.ID);
        group.role = helper.getInt(Ws.GroupTable.ROLE);
        group.desc = helper.getString(Ws.GroupTable.DESC);
        group.group_name = helper.getString(Ws.GroupTable.GROUP_NAME);
        group.school_name = helper.getString(Ws.GroupTable.SCHOOL);
        group.group_members_count = helper.getInt(Ws.GroupTable.GROUP_NUM);
        group.type = helper.getInt(Ws.GroupTable.GROUP_TYPE);
        group.teacher_num = helper.getInt(Ws.GroupTable.TEACHER_NUM);
        group.parent_num = helper.getInt(Ws.GroupTable.PARENT_NUM);
        group.studentnum = helper.getInt(Ws.GroupTable.STUDENT_NUM);
        group.remoteId = helper.getString(Ws.GroupTable.REMOTEID);
        Logger.i("group_id = "+group.group_id + "\nrole "+ group.role + "\ngroup_name = " +group.group_name + "\ngroup = "+group.type);
        return group;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.group_id);
        dest.writeString(this.group_name);
        dest.writeString(this.school_name);
        dest.writeInt(this.group_members_count);
        dest.writeString(this.desc);
        dest.writeInt(this.role);
        dest.writeInt(this.tab);
        dest.writeLong(this.classid);
        dest.writeString(this.remoteId);
        dest.writeInt(this.type);
        dest.writeInt(this.is_shortdn);
        dest.writeInt(this.teacher_num);
        dest.writeInt(this.parent_num);
        dest.writeInt(this.studentnum);
        dest.writeInt(this.isopen_phone);
        dest.writeTypedList(this.group_members);
        dest.writeByte(this.isSelected ? (byte) 1 : (byte) 0);
    }

    public ClazzGroup() {
    }

    protected ClazzGroup(Parcel in) {
        this.group_id = in.readLong();
        this.group_name = in.readString();
        this.school_name = in.readString();
        this.group_members_count = in.readInt();
        this.desc = in.readString();
        this.role = in.readInt();
        this.tab = in.readInt();
        this.classid = in.readLong();
        this.remoteId = in.readString();
        this.type = in.readInt();
        this.is_shortdn = in.readInt();
        this.teacher_num = in.readInt();
        this.parent_num = in.readInt();
        this.studentnum = in.readInt();
        this.isopen_phone = in.readInt();
        this.group_members = in.createTypedArrayList(ClazzContact.CREATOR);
        this.isSelected = in.readByte() != 0;
    }

    public static final Parcelable.Creator<ClazzGroup> CREATOR = new Parcelable.Creator<ClazzGroup>() {
        @Override
        public ClazzGroup createFromParcel(Parcel source) {
            return new ClazzGroup(source);
        }

        @Override
        public ClazzGroup[] newArray(int size) {
            return new ClazzGroup[size];
        }
    };
}
